﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.Mvc;
using Global.Helpers;
using System.Web;
namespace Infrastructure.Web.Mvc.Extensions
{
    public static class UrlHelperExtensions
    {
        //public static string ProductResizedImage(this UrlHelper urlHelper, string path, string filename)
        //{
        //    var resourceName = string.Format("{0}/{1}/{2}", path, ConfigurationManager.AppSettings[GamaOptions.ResizedImageFolderKey], filename);

        //    return ProductImage(urlHelper, resourceName);
        //}

        //public static string ProductResizedImage(this UrlHelper urlHelper, string path, string filename, ImageSize size)
        //{
        //    string extension = System.IO.Path.GetExtension(filename);
        //    string fileName = System.IO.Path.GetFileNameWithoutExtension(filename);
        //    int width = 0, height = 0;
        //    switch (size)
        //    {
        //        case ImageSize.Size75x75:
        //            width = height = 75;
        //            break;
        //        case ImageSize.Size100x100:
        //            width = height = 100;
        //            break;
        //        case ImageSize.Size120x120:
        //            width = height = 120;
        //            break;
        //        case ImageSize.Size128x128:
        //            width = height = 128;
        //            break;
        //        case ImageSize.Size140x140:
        //            width = height = 140;
        //            break;
        //        case ImageSize.Size144x144:
        //            width = height = 144;
        //            break;
        //        case ImageSize.Size204x204:
        //            width = height = 204;
        //            break;
        //        case ImageSize.Size215x215:
        //            width = height = 215;
        //            break;
        //        case ImageSize.Size220x220:
        //            width = height = 220;
        //            break;
        //        case ImageSize.Size264x264:
        //            width = height = 264;
        //            break;
        //    }
        //    var resourceName = string.Format("{0}/{1}/{2}-{3}-{4}{5}"
        //        , path
        //        , ConfigurationManager.AppSettings[GamaOptions.ResizedImageFolderKey]
        //        , fileName
        //        , width
        //        , height
        //        , extension);

        //    return ProductImage(urlHelper, resourceName);
        //}

        public static string ProductImage(this UrlHelper urlHelper, string resourceName)
        {
            return urlHelper.Content(VirtualPathUtility.Combine(GamaOptions.PathToProductImages, resourceName));
        }

        //public static string OptionResizedImage(this UrlHelper urlHelper, string supplierName, string resourceName)
        //{
        //    string pathToOptionImage;

        //    if (!resourceName.Contains(ConfigurationManager.AppSettings[GamaOptions.OptionNoImageFileNameKey]))
        //    {
        //        pathToOptionImage = string.Format("{0}{1}/{2}/{3}/",
        //                                              ConfigurationManager.AppSettings[GamaOptions.PathToProductImagesKey],
        //                                              ConfigurationManager.AppSettings[GamaOptions.OptionImageFolderKey],
        //                                              supplierName,
        //                                              ConfigurationManager.AppSettings[GamaOptions.ResizedImageFolderKey].ToLower());
        //    }
        //    else
        //    {
        //        pathToOptionImage = string.Format("{0}{1}/",
        //                                              ConfigurationManager.AppSettings[GamaOptions.PathToProductImagesKey],
        //                                              ConfigurationManager.AppSettings[GamaOptions.OptionImageFolderKey]);
        //    }

        //    return urlHelper.Content(VirtualPathUtility.Combine(pathToOptionImage, resourceName));
        //}

        //public static string OptionImage(this UrlHelper urlHelper, string path, string resourceName)
        //{
        //    return string.Format("/Content/Images/{0}/{1}/{2}",
        //        ConfigurationManager.AppSettings[GamaOptions.OptionImageFolderKey],
        //        path,
        //        resourceName);
        //}

        public static string LayoutImageDefault(this UrlHelper urlHelper, string resourceName)
        {
            return urlHelper.Content(VirtualPathUtility.Combine(GamaOptions.PathToImages, resourceName));
        }
        public static string LayoutImage(this UrlHelper urlHelper, string resourceName)
        {
            return urlHelper.Content(VirtualPathUtility.Combine(string.Format("{0}{1}/", GamaOptions.PathToImages, HttpContext.Current.Response.Cookies[GamaOptions.LanguageKey].Value), resourceName));
        }        
        public static string PromoImage(this UrlHelper urlHelper, string resourceName)
        {
            if (!string.IsNullOrEmpty(resourceName))
            {
                return urlHelper.Content(VirtualPathUtility.Combine(GamaOptions.PathToPromoImages, resourceName));
            }

            return string.Empty;
        }

        //public static string Style(this UrlHelper urlHelper, string resourceName)
        //{
        //    return urlHelper.Content(VirtualPathUtility.Combine(PathToStyles, resourceName));
        //}

        public static string Style(this UrlHelper urlHelper, string resourceName)
        {

            return urlHelper.Content(string.Format("/Content/Stylesheets/{0}", resourceName.ToLower()));

        }

        public static string Script(this UrlHelper urlHelper, string resourceName)
        {
            return urlHelper.Content(VirtualPathUtility.Combine(GamaOptions.PathToScripts, resourceName));
        }

        /*public static string Script(this UrlHelper urlHelper, string resourceName)
        {
#if !DEBUG
            return urlHelper.Content(string.Format("/resource/script/{0}/{1}", AdhocReplace(ResourceVersion), resourceName.ToLower()));
#else
            return urlHelper.Content(string.Format("/Scripts/{0}", resourceName.ToLower()));
#endif
        }*/

        public static string Media(this UrlHelper urlHelper, string resourceName)
        {
            return urlHelper.Content(VirtualPathUtility.Combine(GamaOptions.PathToMedia, resourceName));
        }

        public static string SubSegment(this UrlHelper urlHelper, string segment)
        {
            return urlHelper.RequestContext.HttpContext.Request.Path.TrimEnd('/') + "/" + segment;
        }

        //public static string GlobalType(this UrlHelper urlHelper)
        //{
        //    return GlobalType(urlHelper, BrowsingType.empty);
        //}

        //public static string GlobalType(this UrlHelper urlHelper, BrowsingType browsingType)
        //{
        //    if (browsingType != BrowsingType.empty)
        //    {
        //        return string.Format("?b={0}", browsingType);
        //    }

        //    var httpRequest = urlHelper.RequestContext.HttpContext.Request;

        //    return string.IsNullOrEmpty(httpRequest.ToString())
        //               ? string.Empty
        //               : string.IsNullOrEmpty(httpRequest["b"])
        //                     ? string.Empty
        //                     : httpRequest.RawUrl.Contains("?b") ? string.Format("?b={0}", httpRequest["b"]) : string.Format("&b={0}", httpRequest["b"]);
        //}

        public static string CurrentFilter(this UrlHelper urlHelper)
        {
            var httpRequest = urlHelper.RequestContext.HttpContext.Request;

            return string.IsNullOrEmpty(httpRequest["N"])
                       ? string.Empty
                       : string.Format("&N={0}", httpRequest["N"].Replace(" ", "+"));
        }

        public static string Page(this UrlHelper urlHelper, int page)
        {
            return String.Format("{0}?page={1}",
                urlHelper.RequestContext.HttpContext.Request.Path.TrimEnd('/'),
                page);
        }
       

        public static string Print(this UrlHelper urlHelper, string path)
        {
            return urlHelper.Content(String.Format("~/Print/{0}", path));
        }
    }
}
